$(function () {

    $("#avatar-btn").click(function () {
        ajaxFileUpload();
    });
    $('#nextstep').click(function () {
        window.location.replace("http://localhost:8080/tutorial/Second.html")

    });
    $("#nextstep").click(function () {
        $.ajax({
            type:"GET",
            url:"/tutorial/nextstep",
            async:true,
            dataType:"json",
            success:function (data) {
                if(data.ok){
                    location.href="/tutorial/index"
                }else {
                    toastr.options = {
                        "closeButton": false,
                        "debug": false,
                        "positionClass": "toast-top-center",
                        "onclick": null,
                        "showDuration": "1000",
                        "hideDuration": "1000",
                        "timeOut": "5000",
                        "extendedTimeOut": "1000",
                        "showEasing": "swing",
                        "hideEasing": "linear",
                        "showMethod": "fadeIn",
                        "hideMethod": "fadeOut"
                    };
                    toastr["warning"]("你目前还无法进入下一章节", "操作失败");
                }
            }
        });
    });

    $.ajax({
        type:"GET",
        url:"/tutorial/problems",
        dataType:"json",
        success:function (data) {
            $("#chapter").text("第"+Arabia_To_SimplifiedChinese(data.tutorial.chapter)+"章 第"
                +Arabia_To_SimplifiedChinese(data.tutorial.section)+"节："+data.tutorial.title);
            $("#description").text(data.tutorial.description);
            $.each(data.tutorial.problems,function (n,value) {

                a="#";
                title = "任务"+(n+1)+":"+value.title;
                $("#task-list").append("<li class='list-group-item' ><span class=\"label label-success pull-right\">完成</span><a href='/solution/submitpage?pid="+value.problemId +"'>"+title+"</a></li>");
            });
            if(data.hasNext){
                $("#nextstep").html("下一章节");
                $("#finish").html("直接下山");
                $("#finish").show();
            }else{
                $("#nextstep").html("下山");
                $("#finish").hide();
            }

        }
    });

    $.ajax({
        type:"GET",
        url:"/user/basicinfo",
        async:true,
        dataType:"json",
        success:function (data) {
            $("#dropdown-user").html(data.nickname+"<span class=" +
                "\"glyphicon glyphicon-user\"></span>" +
                "<span class=\"caret\"></span>");
            $("#header-user-nickname").text(data.nickname);
            $("#header-user-level").text("暂无等级");
            $("#header-user-money").text(data.userInfo.money+"金币");
            $("#header-user-prestige").text("暂无声望");
        }
    })
});

function Arabia_To_SimplifiedChinese(Num) {
    if(Num===0) return "零";
    for (i = Num.length - 1; i >= 0; i--) {
        Num = Num.replace(",", "")//替换Num中的“,”
        Num = Num.replace(" ", "")//替换Num中的空格
    }
    if (isNaN(Num)) { //验证输入的字符是否为数字
        //alert("请检查小写金额是否正确");
        return;
    }
    //字符处理完毕后开始转换，采用前后两部分分别转换
    part = String(Num).split(".");
    newchar = "";
    //小数点前进行转化
    for (i = part[0].length - 1; i >= 0; i--) {
        if (part[0].length > 10) {
            //alert("位数过大，无法计算");
            return "";
        }//若数量超过拾亿单位，提示
        tmpnewchar = ""
        perchar = part[0].charAt(i);
        switch (perchar) {
            case "0":  tmpnewchar = "零" + tmpnewchar;break;
            case "1": tmpnewchar = "一" + tmpnewchar; break;
            case "2": tmpnewchar = "二" + tmpnewchar; break;
            case "3": tmpnewchar = "三" + tmpnewchar; break;
            case "4": tmpnewchar = "四" + tmpnewchar; break;
            case "5": tmpnewchar = "五" + tmpnewchar; break;
            case "6": tmpnewchar = "六" + tmpnewchar; break;
            case "7": tmpnewchar = "七" + tmpnewchar; break;
            case "8": tmpnewchar = "八" + tmpnewchar; break;
            case "9": tmpnewchar = "九" + tmpnewchar; break;
        }
        switch (part[0].length - i - 1) {
            case 0: tmpnewchar = tmpnewchar; break;
            case 1: if (perchar != 0) tmpnewchar = tmpnewchar + "十"; break;
            case 2: if (perchar != 0) tmpnewchar = tmpnewchar + "百"; break;
            case 3: if (perchar != 0) tmpnewchar = tmpnewchar + "千"; break;
            case 4: tmpnewchar = tmpnewchar + "万"; break;
            case 5: if (perchar != 0) tmpnewchar = tmpnewchar + "十"; break;
            case 6: if (perchar != 0) tmpnewchar = tmpnewchar + "百"; break;
            case 7: if (perchar != 0) tmpnewchar = tmpnewchar + "千"; break;
            case 8: tmpnewchar = tmpnewchar + "亿"; break;
            case 9: tmpnewchar = tmpnewchar + "十"; break;
        }
        newchar = tmpnewchar + newchar;
    }
    //替换所有无用汉字，直到没有此类无用的数字为止
    while (newchar.search("零零") != -1 || newchar.search("零亿") != -1 || newchar.search("亿万") != -1 || newchar.search("零万") != -1) {
        newchar = newchar.replace("零亿", "亿");
        newchar = newchar.replace("亿万", "亿");
        newchar = newchar.replace("零万", "万");
        newchar = newchar.replace("零零", "零");
    }
    //替换以“一十”开头的，为“十”
    if (newchar.indexOf("一十") == 0) {
        newchar = newchar.substr(1);
    }
    //替换以“零”结尾的，为“”
    if (newchar.lastIndexOf("零") == newchar.length - 1) {
        newchar = newchar.substr(0, newchar.length - 1);
    }
    return newchar;
}

function ajaxFileUpload() {
    $.ajaxFileUpload({
        type:"POST",
        url:"/user/avatar",
        secureuri:false,
        fileElementId:"file1",
        success:function (data, status) {
            $("#avatar").attr("src","/user/avatar?_="+Math.random());
            $("#modal-avatar").modal('hide');
            toastr.options = {
                "closeButton": false,
                "debug": false,
                "positionClass": "toast-top-center",
                "onclick": null,
                "showDuration": "1000",
                "hideDuration": "1000",
                "timeOut": "1000",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            };
            toastr["info"]("图片上传成功", "操作成功");
        }
    })
}